package main

import (
	"fmt"
)

//#link: https://leetcode-cn.com/problems/valid-number/
func main() {
	s := "3."
	fmt.Println(isNumber(s))
}

func isNumber(s string) bool {
	sByte := []byte(s)
	n, idx := len(sByte), -1

	for i := 0; i < n; i++ {
		if sByte[i] == 'e' || sByte[i] == 'E' {
			if idx == -1 {
				idx = i
			} else {
				return false
			}
		}
	}
	ans := true
	//包含e
	if idx != -1 {
		//左边可以为小数或整数
		ans = ans && check(sByte, 0, idx-1, false)
		//右边只能为整数
		ans = ans && check(sByte, idx+1, n-1, true)
	} else {
		//可以为整数或小数
		ans = ans && check(sByte, 0, n-1, false)
	}
	return ans
}

func check(sByte []byte, start, end int, mustNum bool) bool {
	if start > end {
		return false
	}
	if sByte[start] == '+' || sByte[start] == '-' {
		start++
	}
	hasNum, hasDot := false, false
	for i := start; i <= end; i++ {
		if sByte[i] == '.' {
			if hasDot || mustNum {
				return false
			}
			hasDot = true
		} else if sByte[i] >= '0' && sByte[i] <= '9' {
			hasNum = true
		} else {
			return false
		}
	}
	return hasNum
}
